package menu;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;

import arena.Arena;

public class PanelPlay extends JPanel implements MouseListener,ActionListener{
	private static final long serialVersionUID = 30520100019L;
	public  JTextField 	txtName;
	public  JSlider		sldNumber;
	private JLabel		labelName;
	private JLabel		labelNum;
	private JLabel		labelColor;
	public  JComboBox	cmbColor;
	public  JComboBox   cmbName;
	private JButton		btnPlay;
	private JButton		btnCancel;
	private Font 		f = new Font("IMPACT", Font.PLAIN, 16);
	private String[]    colors = {"black","red","green","gray","blue","yellow"};
	
	public PanelPlay(PanelMain panelMain)
	{
		super(true);
		this.setOpaque(false);
		this.setLayout(new GridLayout(6,0,5,5));
		this.setPreferredSize(new Dimension(300, 50));
		this.setBorder(null);
		sldNumber = new JSlider();
		sldNumber.setSnapToTicks(true);
		sldNumber.setMaximum(6);
		sldNumber.setMinimum(1);
		sldNumber.setMajorTickSpacing(1);
		sldNumber.setMinorTickSpacing(1);
		sldNumber.setPaintTicks(true);
		sldNumber.setPaintLabels(true);
		sldNumber.setForeground(Color.orange);
		sldNumber.setBorder(null);
		sldNumber.setOpaque(false);
		sldNumber.setValue(3);
		sldNumber.setFont(f);
		
		labelName = new JLabel("Player Name:");
		setComponent(labelName);
		labelNum  = new JLabel("Number of players:");
		setComponent(labelNum);
		labelColor = new JLabel("Choose your color:");
		setComponent(labelColor);
		
		txtName = new JTextField();
		txtName.setHorizontalAlignment(JTextField.CENTER);
		txtName.setPreferredSize(new Dimension(100, 20));
		setComponent(txtName);
		txtName.setVisible(true);
		
		cmbColor = new JComboBox(colors);
		setComponent(cmbColor);
		cmbColor.setBackground(Color.blue.darker());
		cmbColor.addActionListener(this);
		
		cmbName = new JComboBox(PanelMain.settings.getPlayers());
		setComponent(cmbName);
		cmbName.setBackground(Color.blue.darker());
		cmbName.setEditable(false);
		cmbName.addActionListener(this);
		cmbName.addMouseListener(this);
		
		btnPlay = new JButton("PLAY");
		setComponent(btnPlay);
		btnPlay.setBorder(null);
		btnPlay.addMouseListener(this);
		
		btnCancel = new JButton("CANCEL");
		setComponent(btnCancel);
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(this);
		
		this.add(labelName);
		this.add(cmbName);
		this.add(labelNum);
		this.add(sldNumber);
		this.add(labelColor);
		this.add(cmbColor);
		this.add(btnPlay);
		this.add(btnCancel);
		this.setVisible(false);
	}

	private void setComponent(JComponent c)
	{
		c.setFont(f);
		c.setBackground(Color.orange);
		c.setOpaque(false);
		c.setAlignmentX(CENTER_ALIGNMENT);
		c.setAlignmentY(CENTER_ALIGNMENT);
		c.setForeground(Color.orange);
		c.setFocusable(true);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource().equals(btnCancel))
		{
			this.setVisible(false);
			return;
		}
		
		if (e.getSource().equals(cmbName) && e.getClickCount()==2)
		{
			this.removeAll();
			this.add(labelName);
			this.add(txtName);
			this.add(labelNum);
			this.add(sldNumber);
			this.add(labelColor);
			this.add(cmbColor);
			this.add(btnPlay);
			this.add(btnCancel);
			repaint();
			txtName.requestFocus();
			return;
		}
		
		if(e.getSource().equals(btnPlay))
		{
			PanelMain.bkSound.close();
			
			PanelMain.settings.playerName  = txtName.getText();
			PanelMain.settings.playerTot   = sldNumber.getValue();
			PanelMain.settings.playerColor = cmbColor.getSelectedIndex();
			PanelMain.settings.updateXML();
			
			//if (true) return;
			
			try {
				new Arena(	"",
							PanelMain.settings.playerColor,
							PanelMain.settings.playerTot,
							PanelMain.settings.playerScreen,
							(PanelMain.settings.playerMusic!=0),
							(PanelMain.settings.playerEffects!=0));
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(cmbName))
		{
			PanelMain.settings.getPlayer(cmbName.getSelectedItem().toString());
			
			sldNumber.setValue(PanelMain.settings.playerTot);
			cmbColor.setSelectedIndex(PanelMain.settings.playerColor);
			
			PanelSetting.getInstance().cmbEffects.setSelectedIndex(PanelMain.settings.playerEffects);
			PanelSetting.getInstance().cmbMusic.setSelectedIndex(PanelMain.settings.playerMusic);
			PanelSetting.getInstance().cmbScreen.setSelectedIndex(PanelMain.settings.playerScreen);
		}
	}
}
